package queue;

import java.util.PriorityQueue;

public class KthLargest {
    PriorityQueue<Integer> heap = null;
    int k;


    public KthLargest(int k, int[] nums) {
        this.k = k;

        //小根堆
        heap = new PriorityQueue<>((a, b) -> a - b);
        for (int x : nums){
            heap.offer(x);

            while (heap.size() > k){
                heap.poll();
            }
        }

    }

    public int add(int val) {
        heap.offer(val);

        while (heap.size() > k){
            heap.poll();
        }

        return heap.peek();
    }
}
